//
//  XuNiView.m
//  夺宝吧
//
//  Created by xiaocai on 15/7/30.
//  Copyright (c) 2015年 FangYuan. All rights reserved.
//

#import "XuNiView.h"
#import "GetNumView.h"
#import "ShowPrizeVC.h"
//确认收货
#define ENSURE_URL @"/api/ApiUser/confirmReceivedGoods"
@interface XuNiView ()<UIAlertViewDelegate>
{
    __weak IBOutlet UIView *bgview;
    //状态lab
    __weak IBOutlet UILabel *slab1;
    __weak IBOutlet UILabel *slab2;
    __weak IBOutlet UILabel *slab3;
    __weak IBOutlet UILabel *slab4;
    __weak IBOutlet UILabel *slab5;
    //状态img
    __weak IBOutlet UIImageView *img1;
    __weak IBOutlet UIImageView *img2;
    __weak IBOutlet UIImageView *img3;
    __weak IBOutlet UIImageView *img4;
    __weak IBOutlet UIImageView *img5;
    
    __weak IBOutlet UIImageView *line1;
    __weak IBOutlet UIImageView *line2;
    __weak IBOutlet UIImageView *line3;
    __weak IBOutlet UIImageView *line4;
    __weak IBOutlet UIImageView *line5;
    __weak IBOutlet UIImageView *line6;
    __weak IBOutlet UIImageView *line7;
    __weak IBOutlet UIImageView *line8;
    
    __weak IBOutlet UILabel *timeLab1;
    __weak IBOutlet UILabel *timeLab2;
    __weak IBOutlet UILabel *timeLab3;
    __weak IBOutlet UILabel *timeLab4;
    __weak IBOutlet UILabel *timeLab5;
    
    __weak IBOutlet UIButton *ensureAddressBtn;
    __weak IBOutlet UIButton *ensureBtn;
    __weak IBOutlet UIButton *waitingBtn;
    
    __weak IBOutlet UIButton *showBtn;
    __weak IBOutlet UILabel *o_nameLab;

    //打码按钮
    __weak IBOutlet UIButton *damaBtn;
    
    //隐藏的
    
    __weak IBOutlet UILabel *xc1;
    GetNumView *getv;
    
    //是否打码标志
    BOOL isDamaflag;
    //记录原始显示值
    NSAttributedString *_ysphone;

}
@end
@implementation XuNiView

- (void)awakeFromNib
{
    CGFloat add = 4;
    self.backgroundColor = QBB_GRAY_COLOR;
    [bgview addSubview:[WYPublic getFenGexian:0 :0 :WYMWITH :0.5]];
    [bgview addSubview:[WYPublic getFenGexian:CGRectGetMinX(slab1.frame) :CGRectGetMaxY(slab1.frame)+add :WYMWITH  :0.5]];
    [bgview addSubview:[WYPublic getFenGexian:CGRectGetMinX(slab2.frame) :CGRectGetMaxY(slab2.frame)+add :WYMWITH  :0.5]];
    [bgview addSubview:[WYPublic getFenGexian:CGRectGetMinX(slab3.frame) :CGRectGetMaxY(slab3.frame)+add :WYMWITH  :0.5]];
    [bgview addSubview:[WYPublic getFenGexian:CGRectGetMinX(slab4.frame) :CGRectGetMaxY(slab4.frame)+add :WYMWITH  :0.5]];
    [bgview addSubview:[WYPublic getFenGexian:0 :CGRectGetMaxY(slab5.frame)+add+1 :WYMWITH  :0.5]];
    
    ensureAddressBtn.hidden = YES;
    ensureBtn.hidden = YES;
    showBtn.hidden = YES;
    waitingBtn.hidden = YES;
    //打码按钮
    [damaBtn setImage:[UIImage imageNamed:@"icon_eye_press"] forState:UIControlStateSelected];
    
    ensureAddressBtn.layer.cornerRadius = 2;
    ensureAddressBtn.layer.masksToBounds = YES;
    ensureAddressBtn.backgroundColor = QBB_RED_COLOR;
    
    ensureBtn.layer.cornerRadius = 2;
    ensureBtn.layer.masksToBounds = YES;
    ensureBtn.backgroundColor = QBB_RED_COLOR;
    showBtn.layer.cornerRadius = 2;
    showBtn.layer.masksToBounds = YES;
    showBtn.backgroundColor = QBB_RED_COLOR;
    
    waitingBtn.layer.cornerRadius = 2;
    waitingBtn.layer.masksToBounds = YES;
    [waitingBtn setTitleColor:QBB_RED_COLOR forState:UIControlStateNormal];
}

- (void)setModel:(RewardModel *)model
{
    _model = model;
    [self changeViews];
    
}
- (void)setIsMineFlag:(BOOL)isMineFlag
{
    _isMineFlag = isMineFlag;
    if (!_isMineFlag) {
        [self hiddenWuLiu];
    }
}
- (void)changeViews
{
   
    if ([_model.reward_virtual_recharge_type class] != [NSNull class]) {
        switch ([_model.reward_virtual_recharge_type intValue]) {
            case 0:
            {
                
            }
                break;
            case 1:
            {
                if ([_model.reward_recharge_qq class] != [NSNull class]) {
                    o_nameLab.attributedText = [WYPublic getRedMiddleStrWithLeft:@"  QQ号码:" Middle:[NSString stringWithFormat:@"%@",_model.reward_recharge_qq] RightStr:@"" Font:14];
                }else{
                    o_nameLab.text = @"  请编辑充值号码";
                }
            }
                break;
            case 2:
            {
                if ([_model.reward_recharge_mobile class] != [NSNull class]) {
                    o_nameLab.attributedText = [WYPublic getRedMiddleStrWithLeft:@"  手机号码:" Middle:[NSString stringWithFormat:@"%@",_model.reward_recharge_mobile] RightStr:@"" Font:14];
                }else{
                    o_nameLab.text = @"  请编辑充值号码";
                }
            }
                break;
            case 3:
            {
                if ([_model.reward_recharge_alipay class] != [NSNull class]) {
                    o_nameLab.attributedText = [WYPublic getRedMiddleStrWithLeft:@"  支付宝账号:" Middle:[NSString stringWithFormat:@"%@",_model.reward_recharge_alipay] RightStr:@"" Font:14];
                }else{
                    o_nameLab.text = @"  请编辑充值号码";
                }
                
            }
                break;
            default:
                break;
        }
    }

    //设置订单状态值
    if ([_model.reward_current_status_value class] != [NSNull class]) {
        switch ([_model.reward_current_status_value integerValue]) {
            case 201:/**************获得奖品,就要确认地址了*****************/
            {
                img2.image = [UIImage imageNamed:@"icon_circle_red"];
                img3.image = [UIImage imageNamed:@"icon_circle_span"];
                img4.image = [UIImage imageNamed:@"icon_circle_span"];
                img5.image = [UIImage imageNamed:@"icon_circle_span"];
                slab3.textColor = [UIColor lightGrayColor];
                slab4.textColor = [UIColor lightGrayColor];
                slab5.textColor = [UIColor lightGrayColor];
                
                line2.image = nil;
                line2.backgroundColor = QBB_RED_COLOR;
                line3.image = [UIImage imageNamed:@"line_2"];
                line4.image = [UIImage imageNamed:@"line_2"];
                line5.image = [UIImage imageNamed:@"line_2"];
                line6.image = [UIImage imageNamed:@"line_2"];
                line7.image = [UIImage imageNamed:@"line_2"];
                line8.image = [UIImage imageNamed:@"line_2"];
                
                timeLab2.hidden = YES;
                timeLab3.hidden = YES;
                timeLab4.hidden = YES;
                timeLab5.hidden = YES;
                if (_isMineFlag) {
                    ensureAddressBtn.hidden = NO;
                }else{
                    UILabel *goLab = [[UILabel alloc]initWithFrame:ensureAddressBtn.frame];
                    goLab.textAlignment = NSTextAlignmentCenter;
                    goLab.font = [UIFont systemFontOfSize:14];
                    goLab.text = @"进行中…";
                    goLab.textColor = QBB_RED_COLOR;
                    [bgview addSubview:goLab];

                }
                
                //取时间
                if ([_model.reward_status_list class] != [NSNull class] && _model.reward_status_list.count > 0) {
                    timeLab1.text = _model.reward_status_list[0][@"time"];
                }
                [self hiddenWuLiu];
            }
                break;
            case 202:/**************确认收货地址,等待派发奖品*****************/
            {
                img3.image = [UIImage imageNamed:@"icon_circle_red"];
                img4.image = [UIImage imageNamed:@"icon_circle_span"];
                img5.image = [UIImage imageNamed:@"icon_circle_span"];
                slab4.textColor = [UIColor lightGrayColor];
                slab5.textColor = [UIColor lightGrayColor];
                
                line4.image = nil;
                line4.backgroundColor = QBB_RED_COLOR;
                line5.image = [UIImage imageNamed:@"line_2"];
                line6.image = [UIImage imageNamed:@"line_2"];
                line7.image = [UIImage imageNamed:@"line_2"];
                line8.image = [UIImage imageNamed:@"line_2"];
                
                waitingBtn.hidden = NO;
                
                timeLab3.hidden = YES;
                timeLab4.hidden = YES;
                timeLab5.hidden = YES;
                
                //取时间
                if ([_model.reward_status_list class] != [NSNull class] && _model.reward_status_list.count > 1) {
                    timeLab1.text = _model.reward_status_list[0][@"time"];
                    timeLab2.text = _model.reward_status_list[1][@"time"];
                }
            }
                break;
            case 203:/**************奖品派发*****************/
            {
                img4.image = [UIImage imageNamed:@"icon_circle_red"];
                img5.image = [UIImage imageNamed:@"icon_circle_span"];
                slab5.textColor = [UIColor lightGrayColor];
                
                line6.image = nil;
                line6.backgroundColor = QBB_RED_COLOR;
                line7.image = [UIImage imageNamed:@"line_2"];
                line8.image = [UIImage imageNamed:@"line_2"];
                
                timeLab4.hidden = YES;
                timeLab5.hidden = YES;
                
                if (_isMineFlag) {
                    ensureBtn.hidden = NO;
                }else{
                    UILabel *goLab = [[UILabel alloc]initWithFrame:ensureBtn.frame];
                    goLab.textAlignment = NSTextAlignmentCenter;
                    goLab.font = [UIFont systemFontOfSize:14];
                    goLab.text = @"进行中…";
                    goLab.textColor = QBB_RED_COLOR;
                    [bgview addSubview:goLab];
                    
                }
                
                
                //取时间
                if ([_model.reward_status_list class] != [NSNull class] && _model.reward_status_list.count > 2) {
                    timeLab1.text = _model.reward_status_list[0][@"time"];
                    timeLab2.text = _model.reward_status_list[1][@"time"];
                    timeLab3.text = _model.reward_status_list[2][@"time"];
                }
                
            }
                break;
            case 204:/**************确认收货*****************/
            {
                img5.image = [UIImage imageNamed:@"icon_circle_red"];
                
                line8.image = nil;
                line8.backgroundColor = QBB_RED_COLOR;
                timeLab5.hidden = YES;
                
                if (_isMineFlag) {
                    showBtn.hidden = NO;
                }else{
                    UILabel *goLab = [[UILabel alloc]initWithFrame:showBtn.frame];
                    goLab.textAlignment = NSTextAlignmentCenter;
                    goLab.font = [UIFont systemFontOfSize:14];
                    goLab.text = @"进行中…";
                    goLab.textColor = QBB_RED_COLOR;
                    [bgview addSubview:goLab];
                    
                }
                
                
                //取时间
                if ([_model.reward_status_list class] != [NSNull class] && _model.reward_status_list.count > 3) {
                    timeLab1.text = _model.reward_status_list[0][@"time"];
                    timeLab2.text = _model.reward_status_list[1][@"time"];
                    timeLab3.text = _model.reward_status_list[2][@"time"];
                    timeLab4.text = _model.reward_status_list[3][@"time"];
                }
                
                
            }
                break;
            case 205:/**************晒单分享*****************/
            {
                img5.image = [UIImage imageNamed:@"icon_circle_red"];
                
                //取时间
                if ([_model.reward_status_list class] != [NSNull class] && _model.reward_status_list.count > 4) {
                    timeLab1.text = _model.reward_status_list[0][@"time"];
                    timeLab2.text = _model.reward_status_list[1][@"time"];
                    timeLab3.text = _model.reward_status_list[2][@"time"];
                    timeLab4.text = _model.reward_status_list[3][@"time"];
                    timeLab5.text = _model.reward_status_list[4][@"time"];
                }
                
            }
                break;
            default:
                break;
        }
    }

}
- (void)hiddenWuLiu
{
    xc1.hidden = YES;
    damaBtn.hidden = YES;
    o_nameLab.hidden = YES;
}
- (void)showWuLiu
{
    if (_isMineFlag) {
        xc1.hidden = NO;
        damaBtn.hidden = NO;
        o_nameLab.hidden = NO;
    }
}
//确认收货地址
- (IBAction)ensureAddressOnClick:(id)sender {
    [self addZheZhao];
    getv = [[[NSBundle mainBundle] loadNibNamed:@"GetNumView" owner:self options:nil]lastObject];
    //    [getv morenzhi:[nameBtn titleForState:UIControlStateNormal]];
    getv.father =self;
    getv.order_id = _model.reward_order_id;
    if ([_model.reward_virtual_recharge_type class] != [NSNull class]) {
        getv.order_type = _model.reward_virtual_recharge_type;
    }
    [self.father.view addSubview:getv];
    getv.frame = CGRectMake(WYMWITH/2 - getv.frame.size.width/2, -1*getv.frame.size.height, getv.frame.size.width, getv.frame.size.height);
    [UIView animateWithDuration:0.15 animations:^{
        getv.center = CGPointMake(WYMWITH/2, WYMHEIGHT/2-100*AUTOY);
    }];
    
}
//添加一个遮罩层
- (void)addZheZhao
{
    UIView *zheZhao = [[UIView alloc]initWithFrame:CGRectMake(0,0,WYMWITH, WYMHEIGHT)];
    zheZhao.tag = 1111;
    zheZhao.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.4];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(bgOnClick)];
    [zheZhao addGestureRecognizer:tap];
    [self.father.view addSubview:zheZhao];
}
- (void)bgOnClick
{
    [self removeZheZhao];
    [getv removeFromSuperview];
}
//去掉遮罩
- (void)removeZheZhao
{
    UIView *zhezhao = [self.father.view viewWithTag:1111];
    [zhezhao removeFromSuperview];
}


- (void)fillNumWithQQ:(NSString *)numqq Phone:(NSString *)phone ZFB:(NSString *)zfb Andtime:(NSString *)time
{
    //改变状态
    _model.reward_current_status_value = [NSNumber numberWithInteger:202];
    _model.reward_current_status = @"确认收货地址";
    _model.reward_recharge_qq = numqq;
    _model.reward_recharge_mobile = phone;
    _model.reward_recharge_alipay = zfb;
    [self changeViews];
    timeLab2.hidden = NO;
    if ([time class] != [NSNull class]) {
        timeLab2.text = time;
    }
    ensureAddressBtn.hidden = YES;
    timeLab3.hidden = YES;
    waitingBtn.hidden = NO;
    
    img2.image = [UIImage imageNamed:@"icon_circle_gray"];
    img3.image = [UIImage imageNamed:@"icon_circle_red"];
    line2.backgroundColor = [UIColor clearColor];
    line2.image = [UIImage imageNamed:@"line_1"];
    line3.image = [UIImage imageNamed:@"line_1"];
    line4.image = nil;
    line4.backgroundColor = QBB_RED_COLOR;
    
    slab3.textColor = [UIColor blackColor];
    [self showWuLiu];
}
//确认收货
- (IBAction)ensurebtnOnclick:(id)sender {
    UIAlertView *alt = [[UIAlertView alloc]initWithTitle:@"提示" message:@"确认您已收到奖品？" delegate:self cancelButtonTitle:@"还未收到" otherButtonTitles:@"已收到", nil];
    [alt show];
}
//确定收到奖品对话框
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == 1){
        [WYPublic addZheZhaoToWindowWithTitle:nil];
        //确认收货
        NSDictionary *params = @{@"record_id":self.model.reward_order_id,@"uid":[UserInfo getUserID],@"token":[UserInfo getUserToken],@"refresh_token":[UserInfo getRefreshToken]};
        [WYRequest startPostForDicWithUrlStR:[NSString stringWithFormat:@"%@%@",DBB_IP,ENSURE_URL] Parameters:params successBlock:^(NSDictionary *dic) {
            NSNumber *ret = dic[@"ret"];
            if ([ret integerValue] == 0) {
                if ([dic[@"data"] class] != [NSNull class]) {
                    timeLab4.hidden = NO;
                    ensureBtn.hidden = YES;
                    timeLab4.text = dic[@"data"];
                    img4.image = [UIImage imageNamed:@"icon_circle_gray"];
                    img5.image = [UIImage imageNamed:@"icon_circle_red"];
                    line6.backgroundColor = [UIColor clearColor];
                    line6.image = [UIImage imageNamed:@"line_1"];
                    line7.image = [UIImage imageNamed:@"line_1"];
                    line8.image = nil;
                    line8.backgroundColor = QBB_RED_COLOR;
                    slab5.textColor = [UIColor blackColor];
                    if (_isMineFlag) {
                        showBtn.hidden = NO;
                    }
                    //改变状态
                    _model.reward_current_status_value = [NSNumber numberWithInteger:204];
                    _model.reward_current_status = @"确认收货";
                }
            }
            [WYPublic removeZheZhaoToWindow];
        } failedBlock:^{
            [WYPublic removeZheZhaoToWindow];
        } needLog:NO];
    }
}

//我要晒单
- (IBAction)showBtnOnclick:(id)sender {
    ShowPrizeVC *showVC = [[ShowPrizeVC alloc]init];
    showVC.model = self.model;
    [[WYMainTabBarVC shareMainTaBarVC].navigationController pushViewController:showVC animated:YES];
}

//打码
- (IBAction)damaBtnOnClick:(id)sender {
    
    if (isDamaflag) {
        o_nameLab.attributedText = _ysphone;
        
    }else{
        _ysphone = o_nameLab.attributedText;
        
        if (o_nameLab.text.length > 10) {
        NSArray *strs = [o_nameLab.text componentsSeparatedByString:@":"];
        if (strs.count>=2) {
            NSString *phone = strs[1];
            phone = [phone stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
            o_nameLab.attributedText = [WYPublic getRedMiddleStrWithLeft:[NSString stringWithFormat:@"%@:",strs[0]] Middle:phone RightStr:@"" Font:14];
            }
        }
    }
    
    isDamaflag = !isDamaflag;
    damaBtn.selected = !damaBtn.selected;
}

@end
